package base64

import "encoding/base64"

func NewBase64(src []byte) *Data {
	encode := Data(base64.StdEncoding.EncodeToString(src))
	return &encode
}

func NewBase64FromString(src string) *Data {
	encode := Data(base64.StdEncoding.EncodeToString([]byte(src)))
	return &encode
}

//base64编码
type Data string

func (d Data) Decode() (string, error) {
	de, err := base64.StdEncoding.DecodeString(string(d))
	if err != nil {
		return "", err
	}
	return string(de), nil
}

func (d Data) MustDecode() string {
	de, err := base64.StdEncoding.DecodeString(string(d))
	if err != nil {
		panic(err.Error())
	}
	return string(de)
}

func (d *Data) DecodeByte() ([]byte, error) {
	de, err := base64.StdEncoding.DecodeString(string(*d))
	if err != nil {
		return nil, err
	}
	return de, nil
}

func (d *Data) MustDecodeByte() []byte {
	de, err := base64.StdEncoding.DecodeString(string(*d))
	if err != nil {
		panic(err.Error())
	}
	return de
}

func (d Data) String() string {
	return string(d)
}
